{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T01:05:09.108129Z",
     "start_time": "2025-01-07T01:05:09.063991Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import random\n",
    "import time"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 列表转化为ndarray",
   "id": "badc5c2a1407666d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#创建列表\n",
    "list1 = [1, 2, 3, 4]\n",
    "print(list1)\n",
    "print(type(list1))\n",
    "\n",
    "#列表转化为ndarray\n",
    "one_dim_array = np.array(list1)\n",
    "print(one_dim_array)\n",
    "print(type(one_dim_array))\n"
   ],
   "id": "a6a11966502fa30"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray与Python原生list运算效率对比",
   "id": "7747e3645a1bd395"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:05:23.706703Z",
     "start_time": "2025-01-07T01:05:15.267832Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#随机1亿个数据\n",
    "list = []\n",
    "for i in range(100000000):\n",
    "    list.append(random.random())\n",
    "print('随机完毕')\n",
    "\n",
    "# 转换为ndarray\n",
    "ndarray = np.array(list)\n",
    "print('转换完毕')"
   ],
   "id": "47cdaa4e1503884f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机完毕\n",
      "转换完毕\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:18:11.058548Z",
     "start_time": "2025-01-07T01:18:10.251376Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#ndarray的计算效率优于python的list\n",
    "t1 = time.time()\n",
    "sum1 = sum(list)\n",
    "t2 = time.time()\n",
    "\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(ndarray)\n",
    "t5 = time.time()\n",
    "print(t2 - t1, t5 - t4)"
   ],
   "id": "bc880275e4a89e73",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6643962860107422 0.13877129554748535\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:21:25.527399Z",
     "start_time": "2025-01-07T01:21:25.524316Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.array()函数可以将列表转化为ndarray，并可以指定数据类型\n",
    "arry1 = np.array([1, 2, 3, 4])\n",
    "print(arry1)\n",
    "print(type(arry1))"
   ],
   "id": "1350b9bb01f371c0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:22:22.251490Z",
     "start_time": "2025-01-07T01:22:22.247917Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# range()函数可以生成一系列数字，返回的是一个range对象，不能直接转化为ndarray\n",
    "print(type(range(10)))\n",
    "# 转化为ndarray\n",
    "arry2 = np.array(range(10))\n",
    "print(arry2)\n",
    "print(type(arry2))"
   ],
   "id": "de736f1674e83d62",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:25:59.680928Z",
     "start_time": "2025-01-07T01:25:59.677516Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.arange()函数可以生成一系列数字，返回的是一个ndarray对象\n",
    "# 等价于range()函数\n",
    "arry3 = np.arange(0, 10, 2)\n",
    "print(arry3)\n",
    "print(type(arry3))"
   ],
   "id": "4ec9521c4f11ae6f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:30:04.327739Z",
     "start_time": "2025-01-07T01:30:04.323830Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二维列表转化为ndarray\n",
    "list2 = [[1, 2, 3], [4, 5, 6]]\n",
    "print(list2)\n",
    "two_dim_array = np.array(list2)\n",
    "\n",
    "# 列表输出有逗号，ndarray输出没有逗号\n",
    "print(two_dim_array)\n",
    "print(type(two_dim_array))\n",
    "\n",
    "# ndarry转化为列表\n",
    "# .tolist()方法可以将ndarray转化为列表\n",
    "list3 = two_dim_array.tolist()\n",
    "print(list3)"
   ],
   "id": "93ec366c9fafbe24",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [4, 5, 6]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "<class 'numpy.ndarray'>\n",
      "[[1, 2, 3], [4, 5, 6]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 常用属性",
   "id": "ac5d253c651fb72c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:35:58.680425Z",
     "start_time": "2025-01-07T01:35:58.675574Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 维度：.ndim属性可以获取ndarray的维度\n",
    "print(two_dim_array.ndim)\n",
    "\n",
    "# 形状：.shape属性可以获取ndarray的形状\n",
    "print(two_dim_array.shape)\n",
    "\n",
    "# 元素个数：.size属性可以获取ndarray的元素个数\n",
    "print(two_dim_array.size)\n",
    "\n",
    "# 元素类型：.dtype属性可以获取ndarray的元素类型\n",
    "print(two_dim_array.dtype)\n"
   ],
   "id": "a68cbbdd9d082560",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(2, 3)\n",
      "6\n",
      "int64\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 调整ndarray形状",
   "id": "52c70ed8d4b18f0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:40:45.778546Z",
     "start_time": "2025-01-07T01:40:45.775548Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#改变新的形状.shape = (2, 3)\n",
    "# id不变，在原ndarry改变\n",
    "new_array = two_dim_array\n",
    "two_dim_array.shape = (2, 3)\n",
    "\n",
    "print(two_dim_array)\n",
    "print(two_dim_array.shape)\n",
    "print(id(two_dim_array))\n",
    "print('-' * 50)\n",
    "\n",
    "print(new_array)\n",
    "print(new_array.shape)\n",
    "print(id(new_array))"
   ],
   "id": "f5a7a3bb65bb55e7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "(2, 3)\n",
      "1924137351376\n",
      "--------------------------------------------------\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "(2, 3)\n",
      "1924137351376\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "4e09cc6247dfab43"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:22:50.777562Z",
     "start_time": "2025-01-07T09:22:50.773361Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#改变新的形状.reshape()方法\n",
    "# id改变\n",
    "new_array = two_dim_array.reshape(3, 2)\n",
    "\n",
    "print(two_dim_array)"
   ],
   "id": "d7267c77bd267c2f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:26:20.759042Z",
     "start_time": "2025-01-07T09:26:20.755538Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将多维变成一维数组\n",
    "# order参数：默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "new_array1 = two_dim_array.reshape((6,), order='C')\n",
    "\n",
    "# .flatten().reshape()方法可以将二维数组展开为一维数组\n",
    "new_array2 = two_dim_array.flatten()\n",
    "\n",
    "print(new_array1)\n",
    "print(new_array2)"
   ],
   "id": "8372fc57113b5ba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Numpy的数据类型",
   "id": "c089f00c2d5509e6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:31:30.580546Z",
     "start_time": "2025-01-07T09:31:30.576652Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#dtype参数：可以指定数据类型\n",
    "# 返回数组中每个元素的字节单位长度\n",
    "f = np.array([1, 2, 3, 4, 127], dtype=np.int8)\n",
    "\n",
    "#itemsize属性：可以获取数组中每个元素的字节单位长度\n",
    "print(f.itemsize)\n",
    "\n",
    "print(f.dtype)"
   ],
   "id": "54bbf7e8bc899caa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "int8\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:32:11.708141Z",
     "start_time": "2025-01-07T09:32:11.704142Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#溢出处理：当数据类型溢出时，会自动截断\n",
    "f[4] = f[4] + 1"
   ],
   "id": "d9f5fff75ac96222",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\35493\\AppData\\Local\\Temp\\ipykernel_14812\\2548770696.py:1: RuntimeWarning: overflow encountered in scalar add\n",
      "  f[4]=f[4]+1\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:33:18.024681Z",
     "start_time": "2025-01-07T09:33:18.020384Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 调整数据类型\n",
    "# astype()方法可以调整数据类型\n",
    "f1 = f.astype(np.int16)\n",
    "print(f1.dtype)\n",
    "print(f1.itemsize)"
   ],
   "id": "9ac9d1ef267d84c4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int16\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:36:32.296990Z",
     "start_time": "2025-01-07T09:36:32.293160Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机生成小数\n",
    "# random.random()函数可以生成0-1之间的随机数\n",
    "# round()函数可以四舍五入\n",
    "print(round(random.random(), 2))\n",
    "\n",
    "#round()等价于np.round()\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "print(np.round(arr, 2))"
   ],
   "id": "c09a8f6d3166a0ac",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.98\n",
      "[0.3  0.98 0.03 0.19 0.46 0.86 0.7  0.56 0.95 0.56]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组和数的计算",
   "id": "cfbc1866bc00911a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:41:17.612326Z",
     "start_time": "2025-01-07T09:41:17.605820Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# numpy的广播机制：当两个数组的形状不一致时，会自动进行广播\n",
    "# 由于numpy的广播机机制在运算过程中，加减乘除的值被广播到所有的元素上面\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "print(f'原始数组:\\n{t1}')\n",
    "print(\"-\" * 20)\n",
    "\n",
    "#tolist()方法可以将ndarray转化为列表\n",
    "t2 = t1.tolist()\n",
    "print(f'原始数字+2：\\n{t1 + 2}')\n",
    "\n",
    "#无论多少维的ndarray都可以直接和一个常数进行运算\n",
    "# print(t2+2) 不能对列表进行直接加整数操作\n",
    "print(\"-\" * 20)\n",
    "print(f'原始数字*2：\\n{t1 * 2}')\n",
    "print(\"-\" * 20)\n",
    "print(f'原始数字/2：\\n{t1 / 2}')"
   ],
   "id": "3cdefb4b82b4a0b0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组:\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "--------------------\n",
      "原始数字+2：\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "原始数字*2：\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "原始数字/2：\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "c66672f90e48a763"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## nadarray与ndarray的运算",
   "id": "9fb9633efe9a6843"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:46:27.141314Z",
     "start_time": "2025-01-07T09:46:27.136803Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# ndarray的运算,要形状相同\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "t2 = np.arange(100, 124).reshape((6, 4))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print('-' * 50)\n",
    "# 加法\n",
    "print(t1 + t2)\n",
    "print('-' * 50)\n",
    "# #不是矩阵乘法\n",
    "print(t1 * t2)"
   ],
   "id": "443fd241ac16a36",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[100 101 102 103]\n",
      " [104 105 106 107]\n",
      " [108 109 110 111]\n",
      " [112 113 114 115]\n",
      " [116 117 118 119]\n",
      " [120 121 122 123]]\n",
      "--------------------------------------------------\n",
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "--------------------------------------------------\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#shape不同，不能进行运算\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(6).reshape((3, 6))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1 - t2)"
   ],
   "id": "b4d11a9f5f339b9e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:50:06.565385Z",
     "start_time": "2025-01-07T09:50:06.561297Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#广播机制，\n",
    "#一维数组和二维数组进行运算时，一维的元素个数和列数相等、二维的行数和列数相等\n",
    "# 广播机制会将一维数组广播到二维数组的每一行，然后进行运算\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(6).reshape((1, 6))\n",
    "print(t2.shape)\n",
    "print(t1)\n",
    "print(t2)\n",
    "t1 - t2"
   ],
   "id": "1fe4d778adc62105",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 6)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0 1 2 3 4 5]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0,  0,  0,  0],\n",
       "       [ 6,  6,  6,  6,  6,  6],\n",
       "       [12, 12, 12, 12, 12, 12],\n",
       "       [18, 18, 18, 18, 18, 18]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:50:08.889797Z",
     "start_time": "2025-01-07T09:50:08.884088Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 广播机制会将一维数组广播到二维数组的每一列，然后进行运算\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(4).reshape((4, 1))\n",
    "print(t2)\n",
    "print(t1)\n",
    "t1 - t2"
   ],
   "id": "dc74b993c552d88d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 5,  6,  7,  8,  9, 10],\n",
       "       [10, 11, 12, 13, 14, 15],\n",
       "       [15, 16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "结论：ndim要相同，可以某一个轴的size不同，但是其中一个ndarray必须对应的轴的size为1",
   "id": "15c9d03612a1cfc"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 轴的操作",
   "id": "725e00eccf7be05a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:54:23.218517Z",
     "start_time": "2025-01-07T09:54:23.213922Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(a.shape)\n",
    "print(\"-\" * 20)\n",
    "\n",
    "# 按哪个轴求和，哪个轴没了\n",
    "print(np.sum(a, axis=0))  # [5 7 9]\n",
    "print(\"-\" * 20)\n",
    "print(np.sum(a, axis=1))  # [ 6 15]\n",
    "print(\"-\" * 20)\n",
    "\n",
    "# 求和所有元素\n",
    "print(np.sum(a))\n",
    "print(\"-\" * 20)"
   ],
   "id": "bd87bb5291d43a9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3)\n",
      "--------------------\n",
      "[5 7 9]\n",
      "--------------------\n",
      "[ 6 15]\n",
      "--------------------\n",
      "21\n",
      "--------------------\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 索引、切片",
   "id": "95d5086c863f4084"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:04:34.183373Z",
     "start_time": "2025-01-07T10:04:34.180113Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# [start:end:step] 左开右闭\n",
    "a = np.arange(10)\n",
    "\n",
    "# 只放置一个索引，返回一个元素\n",
    "print(a[5])\n",
    "\n",
    "# 放置两个索引，返回一个一维数组\n",
    "print(a[2:7])\n",
    "\n",
    "# [2:],返回从索引2开始的所有元素\n",
    "print(a[2:])"
   ],
   "id": "c461444832c25801",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "[2 3 4 5 6]\n",
      "[2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:06:52.777699Z",
     "start_time": "2025-01-07T10:06:52.774194Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#创建二维数组\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "\n",
    "# 取一行\n",
    "print(t1[1])\n",
    "\n",
    "# 取连续多行\n",
    "print(t1[1:3])\n",
    "\n",
    "# 取不连续多行\n",
    "print(t1[[0, 2, 3]])"
   ],
   "id": "d7db21f639804af0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 6  7  8  9 10 11]\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:28:12.547500Z",
     "start_time": "2025-01-07T10:28:12.543080Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 取一列\n",
    "print(t1[:, 1])\n",
    "\n",
    "# 取连续多列\n",
    "print(t1[:, 1:3])\n",
    "\n",
    "# 取不连续多列\n",
    "print(t1[:, [0, 2, 3]])\n",
    "\n",
    "# 取行和列\n",
    "print(t1[1:3, 1:3])"
   ],
   "id": "fb83e7479ef6b5f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  7 13 19]\n",
      "[[ 1  2]\n",
      " [ 7  8]\n",
      " [13 14]\n",
      " [19 20]]\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "[[ 7  8]\n",
      " [13 14]]\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 修改值",
   "id": "a97bbae25fea22f0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:32:37.094298Z",
     "start_time": "2025-01-07T10:32:37.088800Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "\n",
    "#修改某一行的值\n",
    "t[1, :] = 0\n",
    "\n",
    "#修改某一列的值\n",
    "t[:, 2] = 0\n",
    "\n",
    "# 修改连续多行的值\n",
    "t[1:3, :] = 1\n",
    "\n",
    "# 修改连续多列的值\n",
    "t[:, 1:3] = 2\n",
    "\n",
    "# 修改多行多列的值\n",
    "t[1:3, 1:3] = 3\n",
    "\n",
    "#修改多个不相邻的元素\n",
    "t[[0, 2], [0, 2]] = 4\n",
    "\n",
    "# 根据条件修改元素\n",
    "t[t > 10] = 0\n",
    "\n",
    "# 使用逻辑判断修改元素\n",
    "# np.logical_and()函数可以对数组进行与运算\n",
    "# np.logical_or()函数可以对数组进行或运算\n",
    "# np.logical_not()函数可以对数组进行非运算\n",
    "t[np.logical_and(t > 5, t < 15)] = 0\n"
   ],
   "id": "b5f1ce4610de9b75",
   "outputs": [],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:33:49.997497Z",
     "start_time": "2025-01-07T10:33:49.993671Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# python三目运算符也可以修改元素\n",
    "# 三目运算符：a = x if condition else y\n",
    "a = 10\n",
    "b = 15\n",
    "c = a if a > b else b\n",
    "c"
   ],
   "id": "d4b6f86509f77b41",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:35:14.933768Z",
     "start_time": "2025-01-07T10:35:14.929257Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#三目运算（ np.where(condition, x, y)满足条件(condition)，输出x，不满足输出y。)）\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "result = np.where(score < 80, True, False)  #类似于if else\n",
    "print(result)\n",
    "score[result] = 100\n",
    "score"
   ],
   "id": "f28350639d450965",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False]\n",
      " [False False]\n",
      " [ True False]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 80,  88],\n",
       "       [ 82,  81],\n",
       "       [100,  81]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数据的添加、删除与去重",
   "id": "fd17bd6dc0864b94"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:33:21.957265Z",
     "start_time": "2025-01-07T11:33:21.952962Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.append()函数可以添加元素到数组末尾,返回一个新的数组，原数组不变\n",
    "# 输入的数组维度要一致，否则会报错\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(a)\n",
    "\n",
    "# ndarry会被展平，然后再添加元素\n",
    "b = np.append(a, [7, 8, 9])\n",
    "print(b)\n"
   ],
   "id": "79e17c81fb13cbe9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8 9]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:37:19.205121Z",
     "start_time": "2025-01-07T11:37:19.201107Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 沿着某一轴添加元素\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "# 沿轴0添加元素\n",
    "b = np.append(a, [[7, 8, 9]], axis=0)\n",
    "print(b)\n",
    "\n",
    "# 沿轴1添加元素\n",
    "c = np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1)\n",
    "print(c)"
   ],
   "id": "4bca28819a1a97a0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:46:16.656294Z",
     "start_time": "2025-01-07T11:46:16.651412Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.insert()函数可以插入元素到数组中，返回一个新的数组，原数组不变\n",
    "# # 函数给定索引之前，沿给定轴输入数组中插入值，\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "\n",
    "# 未提供轴，输入数组会被展平然后插入\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。')\n",
    "print(np.insert(a, 1, [11, 22]))\n",
    "\n",
    "# print('传递了 Axis 参数。 会广播值数组来配输入数组。')\n",
    "\n",
    "print('沿轴  0 广播：')\n",
    "print(np.insert(a, 1, 11, axis=0))\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴  1 广播：')\n",
    "print(np.insert(a, 1, [1, 2, 5], axis=1))"
   ],
   "id": "1d5f73ba78864b62",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[ 1 11 22  2  3  4  5  6]\n",
      "沿轴  0 广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "沿轴  1 广播：\n",
      "[[1 1 2]\n",
      " [3 2 4]\n",
      " [5 5 6]]\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:48:28.157763Z",
     "start_time": "2025-01-07T11:48:28.153507Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.delete()函数可以删除数组中的元素，返回一个新的数组，原数组不变\n",
    "# 未提供轴，输入数组会被展平然后删除\n",
    "\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "print(a)\n",
    "\n",
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。')\n",
    "print(np.delete(a, 5))\n",
    "\n",
    "print('删除第一行：')\n",
    "print(np.delete(a, 1, axis=0))\n",
    "\n",
    "print('删除第一列：')\n",
    "print(np.delete(a, 1, axis=1))"
   ],
   "id": "6046080153f8de50",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "未传递 Axis 参数。 在删除之前输入数组会被展开。\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "删除第一行：\n",
      "[[ 0  1  2  3]\n",
      " [ 8  9 10 11]]\n",
      "删除第一列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:55:52.677175Z",
     "start_time": "2025-01-07T11:55:52.673175Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.unique()函数可以去除数组中的重复元素，返回一个新的数组，原数组不变\n",
    "'''\n",
    "arr：输入数组，如果不是一维数组则会展平\n",
    "return_index：如果为true，返回新列表元素在旧列表中的位置（下标），并以列表形式储\n",
    "return_inverse：如果为true，返回旧列表元素在新列表中的位置（下标），并以列表形式储\n",
    "return_counts：如果为true，返回去重数组中的元素在原数组中的出现次数\n",
    "'''\n",
    "a = np.array([5, 2, 6, 2, 7, 5, 6, 9, 8, 2])\n",
    "\n",
    "# 去除重复元素，输出的是有序序列\n",
    "print('第一个数组的去重值：')\n",
    "u = np.unique(a)\n",
    "print(u)\n",
    "\n",
    "print('新列表元素在旧列表中的位置（下标）：')\n",
    "u, indices = np.unique(a, return_index=True)\n",
    "print(indices)\n",
    "\n",
    "print('旧列表元素在新列表中的位置（下标）：')\n",
    "u, indices = np.unique(a, return_inverse=True)\n",
    "print(indices)\n",
    "\n",
    "print('去重数组中的元素在原数组中的出现次数：')\n",
    "u, indices = np.unique(a, return_counts=True)\n",
    "print(indices)"
   ],
   "id": "10efbaa0522b625c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组的去重值：\n",
      "[2 5 6 7 8 9]\n",
      "新列表元素在旧列表中的位置（下标）：\n",
      "[1 0 2 4 8 7]\n",
      "旧列表元素在新列表中的位置（下标）：\n",
      "[1 0 2 0 3 1 2 5 4 0]\n",
      "去重数组中的元素在原数组中的出现次数：\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy科学计算",
   "id": "71cb92722881142b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:37.908066Z",
     "start_time": "2025-01-07T12:13:37.902875Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "print(score)\n",
    "\n",
    "# 获得所有数据最大值\n",
    "print(f'所有元素最大值：{np.max(score)}')\n",
    "\n",
    "# 获取每一轴的最大值，对应轴消失\n",
    "print(f'某一轴的最大值：{np.max(score, axis=0)}')\n",
    "\n",
    "# 获取最小值\n",
    "print(f'所有数据最小值：{np.min(score)}')\n",
    "\n",
    "# 获取每一行的最小值\n",
    "print(f'某一轴的最小值：{np.min(score, axis=1)}')\n",
    "\n",
    "# 数据比较\n",
    "# 一个参数中的每一个数与第二个参数比较返回大的，不符合的元素设置为True，然后替换\n",
    "print(f'比较，不符合的元素设置为True{np.maximum([-2, -1, 0, 1, 2], 0)}')\n",
    "\n",
    "# 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(f'比较，返回小的{np.minimum([-2, -1, 0, 1, 2], 0)}')\n",
    "\n",
    "# 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "print(f'比较两个列表，对应元素比较，返回大的{np.maximum([-2, -1, 4, 1, 2], [1, 2, 3, 4, 5])}')"
   ],
   "id": "549cda2eb89f52d4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "所有元素最大值：88\n",
      "某一轴的最大值：[82 88]\n",
      "所有数据最小值：75\n",
      "某一轴的最小值：[80 81 75]\n",
      "比较，不符合的元素设置为True[0 0 0 1 2]\n",
      "比较，返回小的[-2 -1  0  0  0]\n",
      "比较两个列表，对应元素比较，返回大的[1 2 4 4 5]\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''通用函数：\n",
    "numpy.sqrt(array)\t平方根函数\n",
    "numpy.exp(array)\te^array[i]的数组\n",
    "numpy.abs/fabs(array)\t计算绝对值\n",
    "numpy.square(array)\t计算各元素的平方 等于array\t2\n",
    "numpy.log/log10/log2(array)\t计算各元素的各种对数\n",
    "numpy.sign(array)\t计算各元素正负号\n",
    "numpy.isnan(array)\t计算各元素是否为NaN\n",
    "numpy.isinf(array)\t计算各元素是否为NaN\n",
    "numpy.cos/cosh/sin/sinh/tan/tanh(array) 三角函数\n",
    "numpy.modf(array)\t将array中值得整数和小数分离，作两个数组返回\n",
    "numpy.ceil(array)\t向上取整,也就是取比这个数大的整数\n",
    "numpy.floor(array)\t向下取整,也就是取比这个数小的整数\n",
    "numpy.rint(array)\t四舍五入\n",
    "numpy.trunc(array)\t向0取整\n",
    "numpy.cos(array)\t正弦值\n",
    "numpy.sin(array)\t余弦值\n",
    "numpy.tan(array)\t正切值\n",
    "\n",
    "numpy.add(array1,array2)\t元素级加法\n",
    "numpy.subtract(array1,array2)\t元素级减法\n",
    "numpy.multiply(array1,array2)\t元素级乘法\n",
    "numpy.divide(array1,array2)\t元素级除法 array1./array2\n",
    "numpy.power(array1,array2)\t元素级指数 array1.^array2\n",
    "numpy.maximum/minimum(array1,aray2) 元素级最大值\n",
    "numpy.fmax/fmin(array1,array2)\t元素级最大值，忽略NaN\n",
    "numpy.mod(array1,array2)\t元素级求模\n",
    "numpy.copysign(array1,array2)\t将第二个数组中值得符号复制给第一个数组中值\n",
    "numpy.greater/greater_equal/less/less_equal/equal/not_equal (array1,array2)\n",
    "元素级比较运算，产生布尔数组\n",
    "numpy.logical_end/logical_or/logic_xor(array1,array2)元素级的真值逻辑运算\n",
    "'''"
   ],
   "id": "857573c0bb974784"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:18:32.993113Z",
     "start_time": "2025-01-07T12:18:32.989732Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 平均值\n",
    "print(score)\n",
    "# 获得所有数据平均值\n",
    "print(f'所有元素平均值：{np.mean(score)}')\n",
    "\n",
    "# 获取每一轴的平均值\n",
    "print(f'某一轴的平均值：{np.mean(score, axis=0)}')\n",
    "\n",
    "# 求前缀和,前缀和是指将数组中元素从左到右累加，得到的数组\n",
    "print(f'前缀和：{np.cumsum(score)}')\n",
    "print(f'某一轴的前缀和：\\n{np.cumsum(score, axis=0)}')"
   ],
   "id": "7074e27846312fd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "所有元素平均值：81.16666666666667\n",
      "某一轴的平均值：[79.         83.33333333]\n",
      "前缀和：[ 80 168 250 331 406 487]\n",
      "某一轴的前缀和：\n",
      "[[ 80  88]\n",
      " [162 169]\n",
      " [237 250]]\n"
     ]
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:19:52.986181Z",
     "start_time": "2025-01-07T12:19:52.982177Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 求最小值索引:np.argmin(array, axis=None)\n",
    "# 求最大值索引:np.argmax(array, axis=None)\n",
    "print(f'最小值索引：{np.argmin(score)}')\n",
    "print(f'某一轴的最小值索引：{np.argmin(score, axis=0)}')\n",
    "print(f'最大值索引：{np.argmax(score)}')\n",
    "print(f'某一轴的最大值索引：{np.argmax(score, axis=0)}')"
   ],
   "id": "b5e38b895bfda95e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小值索引：4\n",
      "某一轴的最小值索引：[2 1]\n",
      "最大值索引：1\n",
      "某一轴的最大值索引：[1 0]\n"
     ]
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:21:35.812999Z",
     "start_time": "2025-01-07T12:21:35.808948Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标准差：np.std(array, axis=None, dtype=None, out=None, ddof=0, keepdims=False)\n",
    "# 方差：np.var(array, axis=None, dtype=None, out=None, ddof=0, keepdims=False)\n",
    "# 标准差是一组数据离散程度的度量，方差是离散程度的平方，方差越小，数据越集中，标准差越大，数据越分散。\n",
    "print(f'标准差：{np.std(score)}')\n",
    "print(f'某一轴的标准差：{np.std(score, axis=0)}')\n",
    "print(f'方差：{np.var(score)}')\n",
    "print(f'某一轴的方差：{np.var(score, axis=0)}')"
   ],
   "id": "7447f004d06c6969",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "标准差：3.8042374035044424\n",
      "某一轴的标准差：[2.94392029 3.29983165]\n",
      "方差：14.472222222222221\n",
      "某一轴的方差：[ 8.66666667 10.88888889]\n"
     ]
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:23:42.088110Z",
     "start_time": "2025-01-07T12:23:42.084163Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 极值：np.ptp(array, axis=None, out=None, keepdims=False)\n",
    "# 极差：np.diff(array, n=1, axis=-1, prepend=0, append=0)\n",
    "# 极值是指数组中的最大值与最小值的差，极差是指数组中相邻元素的差。\n",
    "print(f'原数组：\\n{score}')\n",
    "print(f'极值：\\n{np.ptp(score)}')\n",
    "print(f'某一轴的极值：\\n{np.ptp(score, axis=0)}')\n",
    "print(f'极差：\\n{np.diff(score)}')\n",
    "print(f'某一轴的极差：\\n{np.diff(score, axis=0)}')"
   ],
   "id": "bd2984e4444f138f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "极值：\n",
      "13\n",
      "某一轴的极值：\n",
      "[7 7]\n",
      "极差：\n",
      "[[ 8]\n",
      " [-1]\n",
      " [ 6]]\n",
      "某一轴的极差：\n",
      "[[ 2 -7]\n",
      " [-7  0]]\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组的拼接与分割",
   "id": "c4f159ef0ba236d8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:02:52.850340Z",
     "start_time": "2025-01-07T13:02:52.846830Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.concatenate()函数可以将多个数组拼接在一起，返回一个新的数组，原数组不变\n",
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[5, 6], [7, 8]])\n",
    "c = np.array([[9, 10], [11, 12]])\n",
    "\n",
    "# 根据轴连接的数组序列，concatenate没有改变数组的维度\n",
    "# 输入的数组维度要一致，否则会报错\n",
    "print('沿轴 0 连接两个数组：')\n",
    "print(np.concatenate((a, b), axis=0))\n",
    "\n",
    "print('沿轴 1 连接两个数组：')\n",
    "print(np.concatenate((a, b, c), axis=1))"
   ],
   "id": "68a5f1ba4cf63e70",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 连接两个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "沿轴 1 连接两个数组：\n",
      "[[ 1  2  5  6  9 10]\n",
      " [ 3  4  7  8 11 12]]\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:08:34.955262Z",
     "start_time": "2025-01-07T13:08:34.950756Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.stack()函数可以将多个数组沿着某一轴拼接在一起，返回一个新的数组，原数组不变\n",
    "# 堆叠,会增加维度\n",
    "print(np.concatenate((a, b, c)).shape)\n",
    "print(np.stack((a, b, c), axis=1).shape)\n",
    "\n",
    "# 根据轴堆叠，2维数组会变成3维数组\n",
    "arrays = [np.arange(12).reshape(3, 4) for _ in range(10)]\n",
    "print('沿轴 0 连接两个数组：')\n",
    "result0 = np.stack(arrays, axis=0)\n",
    "print(result0.shape)  #(10,3,4)\n",
    "\n",
    "print('沿轴 1 连接两个数组：')\n",
    "result1 = np.stack(arrays, axis=1)\n",
    "print(result1.shape)\n",
    "\n",
    "print('沿轴 2连接两个数组：这里-1和2是等价的')\n",
    "result2 = np.stack(arrays, axis=-1)\n",
    "print(result2.shape)"
   ],
   "id": "b05d488bed54b420",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6, 2)\n",
      "(2, 3, 2)\n",
      "沿轴 0 连接两个数组：\n",
      "(10, 3, 4)\n",
      "沿轴 1 连接两个数组：\n",
      "(3, 10, 4)\n",
      "沿轴 2连接两个数组：这里-1和2是等价的\n",
      "(3, 4, 10)\n"
     ]
    }
   ],
   "execution_count": 89
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:10:16.579535Z",
     "start_time": "2025-01-07T13:10:16.575153Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.hstack()函数可以将多个数组沿着水平方向拼接在一起，返回一个新的数组，原数组不变\n",
    "# np.vstack()函数可以将多个数组沿着竖直方向拼接在一起，返回一个新的数组，原数组不变\n",
    "# 维度不变，只是方向不同，类似于np.concatenate()函数\n",
    "\n",
    "# 矩阵垂直拼接\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.vstack((v1, v2))\n",
    "print(result)\n",
    "\n",
    "# 矩阵水平拼接\n",
    "result = np.hstack((v1, v2))\n",
    "print(result)"
   ],
   "id": "f01a75658032fa34",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:15:11.547074Z",
     "start_time": "2025-01-07T13:15:11.542566Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.split()函数可以将数组分割成多个子数组，返回一个列表，原数组不变\n",
    "'''\n",
    "参数说明：\n",
    "ary：被分割的数组\n",
    "indices_or_sections：是一个整数，就用该数平均切分，如果是一个数组，为沿轴切分的位置（左开右闭）\n",
    "axis：沿着哪个维度进行切向，默认为0，横向切分。为1时，纵向切分\n",
    "'''\n",
    "arr = np.arange(12).reshape(4, 3)\n",
    "print(arr)\n",
    "print('将数组分为三个大小相等的子数组：b是一个列表')\n",
    "\n",
    "#沿轴1分割\n",
    "b = np.split(arr, 3, axis=1)\n",
    "print(b)\n",
    "print(b[0].shape)\n",
    "\n",
    "# 沿0轴分割\n",
    "b = np.split(arr, 4, axis=0)\n",
    "print(b)"
   ],
   "id": "fe9063f4637e46b5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "将数组分为三个大小相等的子数组：b是一个列表\n",
      "[array([[0],\n",
      "       [3],\n",
      "       [6],\n",
      "       [9]]), array([[ 1],\n",
      "       [ 4],\n",
      "       [ 7],\n",
      "       [10]]), array([[ 2],\n",
      "       [ 5],\n",
      "       [ 8],\n",
      "       [11]])]\n",
      "(4, 1)\n",
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]]), array([[ 9, 10, 11]])]\n"
     ]
    }
   ],
   "execution_count": 95
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:13:50.797693Z",
     "start_time": "2025-01-07T13:13:50.794185Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "a7055dfb98746668",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]]), array([[ 9, 10, 11]])]\n"
     ]
    }
   ],
   "execution_count": 93
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:18:48.568093Z",
     "start_time": "2025-01-07T13:18:48.564555Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.vsplit()函数可以将数组沿着竖直方向分割成多个子数组，返回一个列表，原数组不变\n",
    "# np.hsplit()函数可以将数组沿着水平方向分割成多个子数组，返回一个列表，原数组不变\n",
    "# 与np.split()函数类似，只是方向不同\n",
    "\n",
    "print(f'原数组：\\n{arr}')\n",
    "# 矩阵垂直分割\n",
    "print(f'竖直分割：\\n{np.vsplit(arr, 2)}')\n",
    "\n",
    "#水平分割数组，通过指定要返回的相同形状的数组数量来拆分原数组。\n",
    "print(f'水平分割：\\n{np.hsplit(arr, 3)}')"
   ],
   "id": "bd64610ce7dbb7b4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "竖直分割：\n",
      "[array([[0, 1, 2],\n",
      "       [3, 4, 5]]), array([[ 6,  7,  8],\n",
      "       [ 9, 10, 11]])]\n",
      "水平分割：\n",
      "[array([[0],\n",
      "       [3],\n",
      "       [6],\n",
      "       [9]]), array([[ 1],\n",
      "       [ 4],\n",
      "       [ 7],\n",
      "       [10]]), array([[ 2],\n",
      "       [ 5],\n",
      "       [ 8],\n",
      "       [11]])]\n"
     ]
    }
   ],
   "execution_count": 100
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "2d7841a4c97c5ad3"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
